package com.rover.system.controller;

import com.grid.DataGrid;
import com.grid.Page;

import com.rover.system.entity.Code;
import com.rover.system.service.CodeService;
import com.utils.HqlGenerateUtil;
import com.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户
 */
@Controller
@RequestMapping("/codeController")
public class CodeController {

    @Autowired
    private CodeService codeService;


    // 用户管理
    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page,
                                     HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            HqlGenerateUtil.installHql(request, Code.class, page);
            DataGrid dataGrid = codeService.query(page);
            map = JsonUtil.JSONString(dataGrid.getResult(),
                    dataGrid.getTotalCount());
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(Code code, HttpServletRequest request)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
            if (null == code.getId()) {
                success = !codeService.isAddNameRepeat(code.getName());
            } else {
                success = !codeService.isEditNameRepeat(code.getId(), code.getName());
            }
            // 判断是否验证成功
            if (success) {
                codeService.saveOrUpdate(code);
            } else {
                message = "此编码名称已存在，请重新输入！";
            }
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            // logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> delete(@RequestParam("id") List<Integer> ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        // String id = request.getParameter("id");
        try {
            codeService.delete(Code.class, ids);
            map.put("success", true);
            map.put("message", "");

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/getList")
    @ResponseBody
    public Map<String, Object> getList(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("success", true);
            List<Code> list = codeService.getList(Code.class);
            for (Code code : list)
                code.setCodeValue(null);
            map.put("rows", list);
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/getCodeId")
    @ResponseBody
    public Map<String, Object> getCodeId(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            map.put("success", true);
            map.put("data", codeService.getCodeIdSpareProperty(request.getParameter("spareProperty")));
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    /*
    * 同步缓存code
    * */
    @RequestMapping(value = "/initAllCode")
    @ResponseBody
    public Map<String, Object> initAllCode(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            codeService.initAllCode();
            map.put("success", true);
            map.put("message", "同步成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
